<?php
/**
 * @title 371. 两整数之和
 * @author start2004
 */

// ini_set("memory_limit", "128M");

// class Solution {
//
//     function find() {
//
//     }
// }

class Solution {

    /**
     * @param Integer $a
     * @param Integer $b
     * @return Integer
     */
    function getSum($a, $b) {
        /**
         * @since 2021-09-26 转二进制
         */
        $binA = decbin($a);
        $binB = decbin($b);
        echo "binA={$binA}, binB={$binB}", PHP_EOL;

        /**
         * @since 2021-09-26 长度
         */
        $lenA = strlen($binA);
        $lenB = strlen($binB);

        /**
         * @since 2021-09-26 二进制数组
         */
        $binArray = [];

        /**
         * @since 2021-09-26 正 + 正
         */
        if($a >=0 AND $b >= 0){
            $addSign = 0;
            for($i=$lenA-1, $j=$lenB-1; $i>=0 AND $j>=0; $i--, $j--){
                $nextSign = 0;
                if($binA[$i] == "1" AND $binB[$j] == "1"){
                    $nextSign = 1;
                    $bin = 0;
                } elseif($binA[$i] == "0" AND $binB[$j] == "0") {
                    $bin = 0;
                } else {
                    $bin = 1;
                }

                /**
                 * @since 2021-09-26 是否进1
                 */
                if($addSign == 1){
                    if($bin == 1){
                        $bin = 0;
                        $nextSign = 1;
                    } else {
                        $bin = 1;
                    }
                } else {}

                /**
                 * @since 2021-09-26 入队
                 */
                $binArray[] = $bin;

                /**
                 * @since 2021-09-26 下次迭代
                 */
                $addSign = $nextSign;
            }

            /**
             * @since 2021-09-26 尾部
             */
            while($i >= 0){
                if($binA[$i] == "1" AND $addSign == 1){
                    $bin = 0;
                    $addSign = 1;
                } elseif($binA[$i] == "0" AND $addSign == 0) {
                    $bin = 0;
                    $addSign = 0;
                } else {
                    $bin = 1;
                    $addSign = 0;
                }
                $binArray[] = $bin;

                $i--;
            }
            while($j >= 0){
                if($binB[$j] == "1" AND $addSign == 1){
                    $bin = 0;
                    $addSign = 1;
                } elseif($binB[$j] == "0" AND $addSign == 0) {
                    $bin = 0;
                    $addSign = 0;
                } else {
                    $bin = 1;
                    $addSign = 0;
                }
                $binArray[] = $bin;

                $j--;
            }

            /**
             * @since 2021-09-26 收官
             */
            if($addSign == 1){
                $binArray[] = 1;
            } else {}

            /**
             * @since 2021-09-26 返回
             */
            $binArray = array_reverse($binArray);

            /**
             * @return
             */
            return bindec(implode("", $binArray));
        } else {}
    }
}

/**
 * @url http://127.0.0.1/leetcode/202109/2021.09.26.php
 */
$datas = [
    [1,2], // 3
    [12, 23], // 3
    // [-2, -3], // -5
];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
foreach ($datas as $data){
    if(count($data) <= 20){
        var_dump($data);
    } else {
        var_dump(count($data));
    }

    $obj = new Solution();
    $result = $obj->getSum(...$data);
    // $result = $obj->($xhprof->tree($data));
    // $result = $obj->($xhprof->listNode($data));
    // $result = $obj->find(...$data);
    var_dump($result);
    // if(count($result)<=20){
    //     var_dump($result);
    // } else {
    //     var_dump(count($result));
    // }
    echo str_repeat(PHP_EOL, 3);
    echo str_repeat("<br>", 3);
}

// foreach ($datas as $data){
//     $obj = new $data[0][0](...$data[1][0]);
//
//     for ($i=1; $i<count($data[0]); $i++){
//         $func = $data[0][$i];
//         $param = $data[1][$i];
//         $result = $obj->$func(...$param);
//
//         echo $data[0][$i] ."(\"". implode(",", $data[1][$i]) ."\") ";
//             if($result === true){
//                 echo "True";
//             } elseif($result === false) {
//                 echo "False";
//             } elseif($result === null) {
//                 echo "Null";
//             } elseif(is_array($result)) {
//                 var_dump($result);
//             } else {
//                 echo $result;
//             }
//         echo PHP_EOL;
//     }
//
//     echo str_repeat(PHP_EOL, 3);
// }
$xhprof->end();
